﻿using Atlass.Framework.Models.Stock;
using Atlass.Framework.Models.Work;
using Atlass.Framework.ViewModels.Common;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Atlass.Framework.AppService.Stock
{
    public class StockRemarkAppService
    {
        private readonly IFreeSql Sqldb;
        public StockRemarkAppService(IServiceProvider service)
        {
            Sqldb = service.GetRequiredService<IFreeSql>();
        }

        public DataTableDto GetPageList(DataTableDto param, int stock_no,string stock_name,string stock_tag)
        {

           var query = Sqldb.Select<stock_remark>()
                .WhereIf(stock_no>0,s=>s.stock_no== stock_no)
                .WhereIf(!string.IsNullOrEmpty(stock_name), s => s.stock_name.Contains(stock_name))
                 .WhereIf(!string.IsNullOrEmpty(stock_tag), s => s.stock_tags.Contains(stock_name))
                .OrderByDescending(s => s.create_time)
                .Count(out long total).
                Page(param.page, param.limit)
                .ToList();
            param.total = total;
            param.rows = query;

            return param;
        }


        public void Save(stock_remark dto)
        {
            dto.stock_no = dto.stock_no;
            dto.stock_name = dto.stock_name ?? "";
            dto.stock_tags = dto.stock_tags ?? "";
            dto.stock_info = dto.stock_info ?? "";
            dto.create_time = DateTime.Now;
            dto.update_time = DateTime.Now;

            if (dto.id == 0)
            {

                Sqldb.Insert(dto).ExecuteAffrows();
                return;
            }

            Sqldb.Update<stock_remark>().SetSource(dto).IgnoreColumns(s => s.create_time).ExecuteAffrows();
        }


        public stock_remark GetModel(int id)
        {
           return  Sqldb.Select<stock_remark>().Where(s => s.id == id).First();
        }
    }
}
